package com.hm.shopping.auto;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.TaskExecutor;
import org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import com.hm.base.service.AuthorizationService;
import com.hm.base.service.SubscriberService;
import com.hm.common.oss.qiniu.su.QiNiuHandlerSupport;
import com.hm.common.util.EncryptUtil;
import com.hm.core.ApiAspectSuport;

import lombok.extern.slf4j.Slf4j;

/**
 * @author shishun.wang
 * @date 上午11:43:29 2017年9月27日
 * @version 1.0
 * @describe
 */
@Slf4j
@EnableAsync // 配置 @Async 异步生效
@Configuration
@SuppressWarnings("unused")
@EnableConfigurationProperties({ SystemEnvConfigProperties.class })
public class HmServerConfiguration {

	@Autowired
	private BeanFactory beanFactory;

	@Autowired
	private SystemEnvConfigProperties systemEnvConfigProperties;

	@Bean
	public TaskExecutor taskExecutor() {
		ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
		executor.setCorePoolSize(10);
		executor.setKeepAliveSeconds(300);
		executor.setMaxPoolSize(15);
		executor.setQueueCapacity(25);
		return executor;
	}

	@Bean
	public ApiAspectSuport apiAspectSuport() {
		return new ApiAspectSuport();
	}

	@Bean
	public QiNiuHandlerSupport qiNiuHandlerSupport() {
		return new QiNiuHandlerSupport(
				EncryptUtil.AES.decrypt(systemEnvConfigProperties.getQiniu().getAccessKey()), 
				EncryptUtil.AES.decrypt(systemEnvConfigProperties.getQiniu().getSecretKey()), 
				EncryptUtil.AES.decrypt(systemEnvConfigProperties.getQiniu().getBucket()))
				.setPrefixUri(EncryptUtil.AES.decrypt(systemEnvConfigProperties.getQiniu().getPrefixUri()));
	}

	/**** rpc invoker call *******/

	@Bean
	public SubscriberService subscriberService() {
		HttpInvokerProxyFactoryBean httpInvokerProxyFactoryBean = new HttpInvokerProxyFactoryBean();
		httpInvokerProxyFactoryBean.setServiceUrl(systemEnvConfigProperties.getHttpInvokerProvider().getManager() + SubscriberService.HTTP_INVOKER_EXPORT_CONTEXT);
		httpInvokerProxyFactoryBean.setServiceInterface(SubscriberService.class);
		httpInvokerProxyFactoryBean.afterPropertiesSet();
		return (SubscriberService) httpInvokerProxyFactoryBean.getObject();
	}

	@Bean
	public AuthorizationService authorizationService() {
		HttpInvokerProxyFactoryBean httpInvokerProxyFactoryBean = new HttpInvokerProxyFactoryBean();
		httpInvokerProxyFactoryBean.setServiceUrl(systemEnvConfigProperties.getHttpInvokerProvider().getManager() + AuthorizationService.HTTP_INVOKER_EXPORT_CONTEXT);
		httpInvokerProxyFactoryBean.setServiceInterface(AuthorizationService.class);
		httpInvokerProxyFactoryBean.afterPropertiesSet();
		return (AuthorizationService) httpInvokerProxyFactoryBean.getObject();
	}
}
